package com.presto.directo.mvvm.viewmodel

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.presto.directo.entity.LoanStatusEntity
import com.presto.directo.net.getHttpMap
import com.presto.directo.net.sign
import com.presto.directo.utils.*

class MainFragmentViewModel(application: Application) : BaseViewModel(application) {

    val statusLiveData = MutableLiveData<LoanStatusEntity>()
    val errorLiveData = MutableLiveData<Int>()

    fun getStatus() {
        start(onNext = {
            showProgress()
            val loanStatusEntity = getApiService().getLoanStatus(getHttpMap().sign(26)).getResult()
            if (loanStatusEntity.loan_status.isNotEmpty() && loanStatusEntity == loanStatus) return@start
            loanStatus = loanStatusEntity
            statusLiveData.value = loanStatus
        }, onError = {
            if (it is MyException) {
                errorLiveData.value = it.code
            }
        }, onComplete = {
            dismissProgress()
        })
    }

}

var loanStatus = emptyStatus()

fun emptyStatus(): LoanStatusEntity {
    return LoanStatusEntity()
}